package com.xadmin.sys.rbac.controller;


import com.xadmin.sys.common.response.Result;
import com.xadmin.sys.common.utils.Page;
import com.xadmin.sys.rbac.pojo.Menu;
import com.xadmin.sys.rbac.pojo.Role;
import com.xadmin.sys.rbac.pojo.RoleAndMenu;
import com.xadmin.sys.rbac.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vic Ly
 */
@RestController
@RequestMapping("/role")
@Api(value = "系统角色模块",tags = "系统角色接口")
public class RoleController {

    @Resource
    private RoleService roleService;

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/roleList",method = RequestMethod.POST)
    @ApiOperation(value = "分页查询角色信息",notes = "分页查询角色信息")
    public Result getByPage(@RequestBody Page<Role> page){
        page = roleService.getAllByPage(page);
        return  Result.ok().data("data",page);
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/getAllRole",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有角色信息",notes = "查询所有角色信息")
    public Result getAllRole(){
        List<Role> roleList = roleService.list();
        return  Result.ok().data("data",roleList);
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @ApiOperation(value = "添加角色",notes = "添加角色")
    public Result addRole(@RequestBody Role role){
        Role u = roleService.getByName(role.getRoleName());
        if(u==null){
            boolean  check =roleService.save(role);
            return  Result.ok().message("添加成功！");
        }else {
            return Result.error().message("用户已存在！");
        }
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/deleteRole/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "根据ID删除角色",notes = "根据ID删除角色")
    public Result deleteRole(@PathVariable Integer id){
        boolean check = roleService.removeById(id);
        if (check == true){
            return  Result.ok().message("删除成功！");
        }else{
            return Result.error().message("删除失败！");
        }
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/viewRole/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据ID查看角色信息",notes = "根据ID查看角色信息")
    public Result viewRole(@PathVariable Integer id){
        Role role = roleService.getById(id);
        return  Result.ok().data("data",role);
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation(value = "根据ID更新角色信息",notes = "根据ID更新角色信息")
    public Result update(@RequestBody Role role){
        boolean check = roleService.updateById(role);
        if (check == true){
            return  Result.ok().message("更新成功！");
        }else{
            return Result.error().message("更新失败！");
        }
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/getMenuByRoleId/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "加载角色的权限菜单",notes = "加载角色的权限菜单（这里是前端的导航显示内容）")
    public Result getMenuByRoleId(@PathVariable Integer id){
        List<Menu> list = roleService.getMenuByRoleId(id);
        return  Result.ok().data("data",list);
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/getMenuByRoleIds/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "加载角色的权限菜单",notes = "加载角色的权限菜单（为了给角色授权）")
    public Result getMenuByRoleIds(@PathVariable Integer id){
        List<Menu> list = roleService.getMenuByRoleIds(id);
        Integer ids[] = new Integer[list.size()];
        int i = 0;
        for(Menu menu: list){
            ids[i] = menu.getId();
            i++;
        }
        return  Result.ok().data("data",ids);
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/addMenus/{id}",method = RequestMethod.POST)
    @ApiOperation(value = "为角色批量添加菜单",notes = "为角色批量添加菜单")
    @Transactional(rollbackFor = Exception.class)
    public Result addMenus(@RequestBody Integer[] ids, @PathVariable Integer id){
        List<RoleAndMenu> idList = new ArrayList<>();
        for(int i = 0;i<ids.length;i++){
            RoleAndMenu ur = new RoleAndMenu();
            ur.setMenuId(ids[i]);
            ur.setRoleId(id);
            idList.add(ur);
        }
        roleService.deleteMenus(id);
        int number = roleService.addMenus(idList);
        if (number != 0){
            return  Result.ok().message("分配成功！");
        }else{
            return Result.error().message("分配失败！");
        }
    }

}

